<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>oop的练习</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }

        #app{
            width: 1090px;
            margin: 50px auto;
        }
        p{
            margin: 50px;
            font-size: 25px;
        }
        input{
            width: 300px;
            height: 30px;
            margin-left: 150px;
        }
        button{
            width: 100px;
            height: 35px;
            line-height: 30px;
        }
        #ulList{
            width: 800px;
            min-height: 300px;
            background: antiquewhite;
            list-style: none;
            margin: 50px auto;
        }

        .myLi{
            width: 100%;
            height: 30px;
            line-height: 30px;
            font-weight: bold;
            background: black;
            color: #ffffff;
            font-size: 18px;
            text-indent: 10px;
            margin-top: 10px;
        }
    </style>
</head>
<body>
<div id="app">

    <p>实现一个需求,在输入框中输入内容的时候,将内容添加到ul中 提示 li</p>

    <input type="text" id="name">
    <button id="add">添加</button>

    <ul id="ulList">

    </ul>
</div>

<script>
    //先使用普通写法
    let addT = document.getElementById("add");
    let ulList = document.getElementById("ulList");
    let name = document.getElementById("name");


    name.onkeyup = ((e) => {
        if(e.keyCode == 13){
            doAppend();
        }
    })
    addT.onclick = (() => {
        doAppend();
    });


    function doAppend(){
        let nameValue = name.value;
        if(nameValue.length == 0){
            alert("不能为空...");
            return false;
        }
        //创建标签
        let fragment = createEle('li',nameValue);
        ulList.append(fragment);
        name.value = "";
    }


    function createEle(targetEl,nameValue){
        //append追加元素，到尾部
        var fragment = document.createDocumentFragment();
        let li_ele = document.createElement(targetEl);
        li_ele.className = 'myLi';
        li_ele.innerText = nameValue;
        fragment.appendChild(li_ele)
        return fragment;
    }
</script>
</body>
</html>